package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.mapper.LikeStrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.service.ITagService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private LikeStrategyCommentMapper likeStrategyCommentMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        return null;
    }


    //保存标签和点评
    @Override
    public void saveComment(StrategyComment comment) {
        String[] tagNames = comment.getTagNames();
        comment.setCommendTime(new Date());
        comment.setCreateTime(new Date());
        strategyCommentMapper.insert(comment);
        //判断属于新增的标签还是系统原标签
        for(int i = 0; i< tagNames.length; i++){
            Tag tag = tagMapper.selectByTagName(tagNames[i]);
            if (tag!=null){
                strategyCommentMapper.insertRelationWithTags(comment.getId(),tag.getId());
            }else{
                Tag tag1 = new Tag(null, tagNames[i]);
                tagMapper.insert(tag1);
                strategyCommentMapper.insertRelationWithTags(comment.getId(),tag1.getId());
            }
        }
    }



    @Override
    public StrategyComment selectStrategyCommentById(Long id) {
        return strategyCommentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyComment> selectCommentsByStrategyId(Long strategyId) {
        return strategyCommentMapper.selectByStrategyId(strategyId);
    }

    /*@Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Tag> list = strategyCommentService.selectForList(qo);
        return new PageInfo(list);
    }*/
    @Override
    public int queryIfexist(Long strategyCommentId, Long userId) {
        return likeStrategyCommentMapper.queryIfexist(strategyCommentId,userId);
    }

    @Override
    public void saveLike(Long userId, Long strategyCommentId) {
        likeStrategyCommentMapper.insert(strategyCommentId,userId,new Date());
    }

    @Override
    public void deleteLike(Long userId, Long strategyCommentId) {
        likeStrategyCommentMapper.deleteByPrimaryKey(strategyCommentId,userId);
    }

    @Override
    public int queryLikeCount(Long strategyCommentId) {
        return likeStrategyCommentMapper.queryLikeCount(strategyCommentId);
    }

    @Override
    public PageInfo queryForLikeMe(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Map> list = likeStrategyCommentMapper.queryForLikeMe(qo);
        return new PageInfo(list);
    }
}
